const {
    addfriend,
    getfriends,
    getcount,
    auditFriend,
    getOnefriend,
    searchFriend,
    deletee,
    updatefriend
} = require('../serve/friend');
let { deleteList, deleteimg } = require("../util/qiniuUpload.js");
const Response = require("../util/respanse.js")
const addfriends = async (req, res) => {
    let { friend_name, friend_url, friend_corver, friend_sign, friend_note } = req.body
    console.log({ friend_name, friend_url, friend_corver, friend_sign, friend_note } );
    let ress = await addfriend({ friend_name, friend_url, friend_corver, friend_sign, friend_note })
    if (ress.code) {
        Response.successresponse(res, {
            code: 200,
            message: ress.msg,
        })
    } else {
        Response.errorresponse(res, {
            code: 400,
            msg: ress.msg,

        })

    }
}
/* 获取友链 */
const getfriend = async (req, res) => {
    let { page, pageSize, friend_status } = req.query
    let ress = await getfriends({ page, pageSize, friend_status })
    if (ress.code) {
        Response.successresponse(res, {
            code: 200,
            message: ress.msg,
            data: ress.data
        })
    } else {
        Response.errorresponse(res, {
            code: 400,
            msg: ress.msg,
        })

    }
}
/* 获取数量 */
const getgountfrient = async (req, res) => {
    try {
        let { friend_status } = req.query
        let ress = await getcount(friend_status)
        if (ress.code) {
            Response.successresponse(res, {
                message: ress.msg,
                code: 200,
                data: ress.data
            })
        } else {
            Response.successresponse(res, {
                code: 200,
                message: ress.msg,
                data: ress.data
            })
        }
    } catch (error) {
        console.log(err);
        Response.errorresponse(res, {
            code: 400,
            msg: "出错了",
            err: error
        })

    }
}
/* 审核 */
const auditFriends = async (req, res) => {
    let { id, fleg } = req.body
    try {
        if (!id || !fleg) {
            Response.errorresponse(res, {
                code: 400,
                msg: "参数不正确"
            })
        }
        let rees = await getOnefriend(id)
        if (rees.code) {
            let ress = await auditFriend(id, fleg)
            if (ress.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: ress.msg,
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: ress.msg
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "该好友不存在"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 搜索 */
const searchfriend = async (req, res) => {
    try {
        let { keyword, page, pageSize, fleg } = req.body
        let ress = await searchFriend(keyword, page, pageSize, fleg)
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: ress.msg,
                data: ress.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: ress.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 删除 */
const deleteFriend = async (req, res) => {
    try {
        let { friendlist } = req.body
        let idlist
        let rees = []
        for (let i = 0; i < friendlist.length; i++) {
            rees.push(getOnefriend(friendlist[i].id))
        }
        let result = await Promise.all(rees)
        idlist = result.map(item => {
            return item.data.id
        })
        let imglist = result.map(item => {
            return item.data.friend_corver.split("/").pop()
        })
        imglist.forEach(item => {
            deleteimg(item, async (err, respBody, respInfo) => {
                if (err) {
                    console.log(err);
                } else {
                    console.log(respInfo.statusCode);
                }
            })
        })
        if (idlist.length === 0) {
            Response.errorresponse(res, {
                code: 400,
                msg: "数据为空",
            })
        } else {
            let ress = await deletee(idlist)
            if (ress.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: ress.msg,
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: ress.msg
                })
            }
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 编辑 */
const updatelinks = async (req, res) => {
    try {
        let { id, data } = req.body
        if (!id || !data || typeof data !== 'object') {
            Response.errorresponse(res, {
                code: 400,
                msg: "参数错误"
            })
            return
        }
        let rees = await getOnefriend(id)
        if (rees.code) {
            let ress = await updatefriend(id, data)
            if (ress.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: ress.msg,
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: ress.msg
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "该好友不存在"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 删除照片 */
const deleteimgname = async (req, res) => {
    try {
        let { oldimg } = req.body
        if (oldimg !== "2024011919245858472.jpg") {
            deleteimg(oldimg, async (err, respBody, respInfo) => {
                if (err) {
                    Response.errorresponse(res, {
                        code: 400,
                        msg: "删除失败",
                    })
                } else {
                    if (respInfo.statusCode == 200) {
                        Response.successresponse(res, {
                            code: 200,
                            message: "成功",
                        })
                    } else {
                        Response.errorresponse(res, {
                            code: 400,
                            msg: "删除失败",
                        })
                    }
                }
            })
        } else {
            Response.successresponse(res, {
                code: 200,
                message: "成功",
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
module.exports = {
    addfriends,
    getfriend,
    getgountfrient,
    auditFriends,
    searchfriend,
    deleteFriend,
    updatelinks,
    deleteimgname
}